package com.example.administrator.chesstest01;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UsersListActivity extends BaseActivity {

    
    private String TAG = "UsersJo";
    @BindView(R.id.btn_flash) Button btnFlash;
    @BindView(R.id.lv_users_list) ListView lvUsersList;
    private ArrayList<RoomMessageItem> arrayList = null;
    private MyAdapter adapter = null;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case Common.JsonCmdResp.APPLY_FOR_ROOM_LIST:
                    AnalysisJson.AnalysisRoomList(arrayList,msg.obj.toString());
                    adapter.notifyDataSetChanged();
                    break;
                case Common.JsonCmdResp.APPLY_FOR_JOIN_ROOM:
                    try {
                        JSONObject jo = new JSONObject(msg.obj.toString());
                        if (jo.getInt("status") == 0){
                            room_id = AnalysisJson.ExitRoomId(msg.obj.toString(),room_id);
                            Intent intentChess = new Intent(UsersListActivity.this,ChessRoomActivity.class);
                            intentChess.putExtra("room_id",room_id);
                            startActivity(intentChess);
                        }
                        else{Toast.makeText(UsersListActivity.this, "房间人数已满", Toast.LENGTH_SHORT).show();}
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获得service中binder对象
            binder = (MyService.OptBinder) service;
            binder.setHandler(handler);
            AnalysisJson.ApplyForRoomList(binder);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            binder = null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_users_list);
        ButterKnife.bind(this);
        Intent intent = new Intent(this,MyService.class);
        bindService(intent,conn, 0);
        arrayList = new ArrayList<>();
        adapter = new MyAdapter(UsersListActivity.this,arrayList);
        lvUsersList.setAdapter(adapter);
        btnFlash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnalysisJson.ApplyForRoomList(binder);
                adapter.notifyDataSetChanged();
            }
        });
        lvUsersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != binder){
                    AnalysisJson.JoinRoom(arrayList,binder,position);
                }
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //activity消亡解除绑定
        if (null != conn){
            unbindService(conn);
        }
    }
}
